package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.BackgroundColorAdapter;
import flc.ast.adapter.BackgroundImageAdapter;
import flc.ast.adapter.RatioAdapter;
import flc.ast.bean.MyBackgroundColorBean;
import flc.ast.bean.MyBackgroundImageBean;
import flc.ast.bean.MyRatioBean;
import flc.ast.databinding.ActivityFreeSpliceBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class FreeSpliceActivity extends BaseAc<ActivityFreeSpliceBinding> {

    public static List<String> listPath = new ArrayList<>();
    private BackgroundColorAdapter colorAdapter;
    private int oldColorPos = 0;
    private BackgroundImageAdapter imageAdapter;
    private int oldImagePos = 0;
    private RatioAdapter ratioAdapter;
    private int oldRatioPos = 0;
    private int viewHeight = 0;
    private int viewWidth = 0;

    @Override
    protected int onCreate() {
        return R.layout.activity_free_splice;
    }

    @Override
    protected void initView() {
        mDataBinding.ivFreeSpliceBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvFreeSpliceSave.setOnClickListener(this);
        mDataBinding.tvFreeSpliceKind1.setOnClickListener(this);
        mDataBinding.tvFreeSpliceKind2.setOnClickListener(this);
        mDataBinding.tvFreeSpliceKind3.setOnClickListener(this);
        mDataBinding.tvFreeSpliceKind2Kind1.setOnClickListener(this);
        mDataBinding.tvFreeSpliceKind2Kind2.setOnClickListener(this);
        //纯色
        mDataBinding.rvColorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        colorAdapter = new BackgroundColorAdapter();
        mDataBinding.rvColorList.setAdapter(colorAdapter);
        colorAdapter.setOnItemClickListener(this);
        //图片
        mDataBinding.rvImageList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        imageAdapter = new BackgroundImageAdapter();
        mDataBinding.rvImageList.setAdapter(imageAdapter);
        imageAdapter.setOnItemClickListener(this);
        //比例
        mDataBinding.rvRatioList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        ratioAdapter = new RatioAdapter();
        mDataBinding.rvRatioList.setAdapter(ratioAdapter);
        ratioAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvFreeSpliceSave://保存
                saveImg();
                break;
            case R.id.tvFreeSpliceKind1://背景
                clearSelection();
                mDataBinding.ivFreeSpliceKind1Sel.setVisibility(View.VISIBLE);
                mDataBinding.llFreeSpliceView1.setVisibility(View.VISIBLE);
                mDataBinding.llFreeSpliceView2.setVisibility(View.GONE);
                break;
            case R.id.tvFreeSpliceKind2://比例
                clearSelection();
                mDataBinding.ivFreeSpliceKind2Sel.setVisibility(View.VISIBLE);
                mDataBinding.llFreeSpliceView1.setVisibility(View.GONE);
                mDataBinding.llFreeSpliceView2.setVisibility(View.VISIBLE);
                break;
            case R.id.tvFreeSpliceKind3://添加
                if (listPath.size() == 9) {
                    ToastUtils.showShort(getString(R.string.max_sel_nine));
                    return;
                }
                Intent intent = new Intent(this, SelPictureActivity.class);
                intent.putExtra(Content.Again, "Again");
                startActivityForResult(intent, Content.SPLICE_ADD);
                break;
            case R.id.tvFreeSpliceKind2Kind1://纯色
                mDataBinding.tvFreeSpliceKind2Kind1.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvFreeSpliceKind2Kind2.setTextColor(Color.parseColor("#66FFFFFF"));
                mDataBinding.ivFreeSpliceKind2Kind1Sel.setVisibility(View.VISIBLE);
                mDataBinding.ivFreeSpliceKind2Kind2Sel.setVisibility(View.GONE);
                mDataBinding.rvColorList.setVisibility(View.VISIBLE);
                mDataBinding.rvImageList.setVisibility(View.GONE);
                break;
            case R.id.tvFreeSpliceKind2Kind2://图案
                mDataBinding.tvFreeSpliceKind2Kind1.setTextColor(Color.parseColor("#66FFFFFF"));
                mDataBinding.tvFreeSpliceKind2Kind2.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.ivFreeSpliceKind2Kind1Sel.setVisibility(View.GONE);
                mDataBinding.ivFreeSpliceKind2Kind2Sel.setVisibility(View.VISIBLE);
                mDataBinding.rvColorList.setVisibility(View.GONE);
                mDataBinding.rvImageList.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == colorAdapter) {
            imageAdapter.getItem(oldImagePos).setSelect(false);
            imageAdapter.notifyItemChanged(oldImagePos);
            colorAdapter.getItem(oldColorPos).setSelect(false);
            colorAdapter.notifyItemChanged(oldColorPos);
            oldColorPos = position;
            colorAdapter.getItem(position).setSelect(true);
            colorAdapter.notifyItemChanged(position);
            mDataBinding.rlFreeSpliceView.setBackgroundColor(Color.parseColor(colorAdapter.getItem(position).getColor()));
        } else if (adapter == imageAdapter) {
            colorAdapter.getItem(oldColorPos).setSelect(false);
            colorAdapter.notifyItemChanged(oldColorPos);
            imageAdapter.getItem(oldImagePos).setSelect(false);
            imageAdapter.notifyItemChanged(oldImagePos);
            oldImagePos = position;
            imageAdapter.getItem(position).setSelect(true);
            imageAdapter.notifyItemChanged(position);
            mDataBinding.rlFreeSpliceView.setBackgroundResource(imageAdapter.getItem(position).getImg());
        } else if (adapter == ratioAdapter) {
            ratioAdapter.getItem(oldRatioPos).setSelect(false);
            ratioAdapter.notifyItemChanged(oldRatioPos);
            oldRatioPos = position;
            ratioAdapter.getItem(position).setSelect(true);
            ratioAdapter.notifyItemChanged(position);
            setRatio(ratioAdapter.getItem(position).getHeight(), ratioAdapter.getItem(position).getWidth());
        }
    }

    @Override
    protected void initData() {
        if (listPath.size() == 0) return;
        //纯色数据
        List<MyBackgroundColorBean> listColor = new ArrayList<>();
        listColor.add(new MyBackgroundColorBean("#FFFFFF", true));
        listColor.add(new MyBackgroundColorBean("#D8D8D8", false));
        listColor.add(new MyBackgroundColorBean("#2D2D2D", false));
        listColor.add(new MyBackgroundColorBean("#FF8F8F", false));
        listColor.add(new MyBackgroundColorBean("#FFBE46", false));
        listColor.add(new MyBackgroundColorBean("#FFEB47", false));
        listColor.add(new MyBackgroundColorBean("#9BDC3D", false));
        listColor.add(new MyBackgroundColorBean("#45C965", false));
        listColor.add(new MyBackgroundColorBean("#4FADB6", false));
        listColor.add(new MyBackgroundColorBean("#3C49DD", false));
        listColor.add(new MyBackgroundColorBean("#6140AA", false));
        listColor.add(new MyBackgroundColorBean("#C050DE", false));
        listColor.add(new MyBackgroundColorBean("#E74F8E", false));
        listColor.add(new MyBackgroundColorBean("#C82222", false));
        colorAdapter.setList(listColor);
        //图片数据
        List<MyBackgroundImageBean> listImage = new ArrayList<>();
        listImage.add(new MyBackgroundImageBean(R.drawable.cc1, false));
        listImage.add(new MyBackgroundImageBean(R.drawable.cc2, false));
        listImage.add(new MyBackgroundImageBean(R.drawable.cc3, false));
        listImage.add(new MyBackgroundImageBean(R.drawable.cc4, false));
        listImage.add(new MyBackgroundImageBean(R.drawable.cc5, false));
        listImage.add(new MyBackgroundImageBean(R.drawable.cc6, false));
        listImage.add(new MyBackgroundImageBean(R.drawable.cc7, false));
        listImage.add(new MyBackgroundImageBean(R.drawable.cc8, false));
        listImage.add(new MyBackgroundImageBean(R.drawable.cc9, false));
        listImage.add(new MyBackgroundImageBean(R.drawable.cc10, false));
        listImage.add(new MyBackgroundImageBean(R.drawable.cc11, false));
        imageAdapter.setList(listImage);
        //比例数据
        List<MyRatioBean> listRatio = new ArrayList<>();
        listRatio.add(new MyRatioBean(0, -200, "1:1", R.drawable.shape_ratio_11_bg, R.drawable.shape_ratio_11_sel, false));
        listRatio.add(new MyRatioBean(0, -350, "4:3", R.drawable.shape_ratio_43_bg, R.drawable.shape_ratio_43_sel, false));
        listRatio.add(new MyRatioBean(0, 100, "3:4", R.drawable.shape_ratio_34_bg, R.drawable.shape_ratio_34_sel, false));
        listRatio.add(new MyRatioBean(0, -500, "16:9", R.drawable.shape_ratio_169_bg, R.drawable.shape_ratio_169_sel, false));
        listRatio.add(new MyRatioBean(-200, 0, "9:16", R.drawable.shape_ratio_916_bg, R.drawable.shape_ratio_916_sel, false));
        ratioAdapter.setList(listRatio);
        //设置图片
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setSticker();
                viewHeight = mDataBinding.rlFreeSpliceView.getHeight();
                viewWidth = mDataBinding.rlFreeSpliceView.getWidth();
            }
        }, 500);
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.saving));
        mDataBinding.ivStickerView1.setShowHelpToolFlag(false);
        mDataBinding.ivStickerView2.setShowHelpToolFlag(false);
        mDataBinding.ivStickerView3.setShowHelpToolFlag(false);
        mDataBinding.ivStickerView4.setShowHelpToolFlag(false);
        mDataBinding.ivStickerView5.setShowHelpToolFlag(false);
        mDataBinding.ivStickerView6.setShowHelpToolFlag(false);
        mDataBinding.ivStickerView7.setShowHelpToolFlag(false);
        mDataBinding.ivStickerView8.setShowHelpToolFlag(false);
        mDataBinding.ivStickerView9.setShowHelpToolFlag(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {
                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlFreeSpliceView);
                        String resultFile = FileUtil.generateFilePath(Content.myCreate, ".png");
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        ImageUtils.save(bitmap, resultFile, Bitmap.CompressFormat.PNG);
                        emitter.onNext(resultFile);
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        FreeSpliceResultActivity.imgPath = path;
                        startActivity(FreeSpliceResultActivity.class);
                    }
                });
            }
        }, 1000);
    }

    //设置比例
    private void setRatio(int height, int width) {
        ViewGroup.LayoutParams params = mDataBinding.rlFreeSpliceView.getLayoutParams();
        params.height = viewHeight + height;
        params.width = viewWidth + width;
        mDataBinding.rlFreeSpliceView.setLayoutParams(params);
    }

    //设置图片
    private void setSticker() {
        switch (listPath.size()) {
            case 1:
                mDataBinding.ivStickerView1.addBitImage(ImageUtils.getBitmap(listPath.get(0)));
                break;
            case 2:
                mDataBinding.ivStickerView1.addBitImage(ImageUtils.getBitmap(listPath.get(0)));
                mDataBinding.ivStickerView2.addBitImage(ImageUtils.getBitmap(listPath.get(1)));
                break;
            case 3:
                mDataBinding.ivStickerView1.addBitImage(ImageUtils.getBitmap(listPath.get(0)));
                mDataBinding.ivStickerView2.addBitImage(ImageUtils.getBitmap(listPath.get(1)));
                mDataBinding.ivStickerView3.addBitImage(ImageUtils.getBitmap(listPath.get(2)));
                break;
            case 4:
                mDataBinding.ivStickerView1.addBitImage(ImageUtils.getBitmap(listPath.get(0)));
                mDataBinding.ivStickerView2.addBitImage(ImageUtils.getBitmap(listPath.get(1)));
                mDataBinding.ivStickerView3.addBitImage(ImageUtils.getBitmap(listPath.get(2)));
                mDataBinding.ivStickerView4.addBitImage(ImageUtils.getBitmap(listPath.get(3)));
                break;
            case 5:
                mDataBinding.ivStickerView1.addBitImage(ImageUtils.getBitmap(listPath.get(0)));
                mDataBinding.ivStickerView2.addBitImage(ImageUtils.getBitmap(listPath.get(1)));
                mDataBinding.ivStickerView3.addBitImage(ImageUtils.getBitmap(listPath.get(2)));
                mDataBinding.ivStickerView4.addBitImage(ImageUtils.getBitmap(listPath.get(3)));
                mDataBinding.ivStickerView5.addBitImage(ImageUtils.getBitmap(listPath.get(4)));
                break;
            case 6:
                mDataBinding.ivStickerView1.addBitImage(ImageUtils.getBitmap(listPath.get(0)));
                mDataBinding.ivStickerView2.addBitImage(ImageUtils.getBitmap(listPath.get(1)));
                mDataBinding.ivStickerView3.addBitImage(ImageUtils.getBitmap(listPath.get(2)));
                mDataBinding.ivStickerView4.addBitImage(ImageUtils.getBitmap(listPath.get(3)));
                mDataBinding.ivStickerView5.addBitImage(ImageUtils.getBitmap(listPath.get(4)));
                mDataBinding.ivStickerView6.addBitImage(ImageUtils.getBitmap(listPath.get(5)));
                break;
            case 7:
                mDataBinding.ivStickerView1.addBitImage(ImageUtils.getBitmap(listPath.get(0)));
                mDataBinding.ivStickerView2.addBitImage(ImageUtils.getBitmap(listPath.get(1)));
                mDataBinding.ivStickerView3.addBitImage(ImageUtils.getBitmap(listPath.get(2)));
                mDataBinding.ivStickerView4.addBitImage(ImageUtils.getBitmap(listPath.get(3)));
                mDataBinding.ivStickerView5.addBitImage(ImageUtils.getBitmap(listPath.get(4)));
                mDataBinding.ivStickerView6.addBitImage(ImageUtils.getBitmap(listPath.get(5)));
                mDataBinding.ivStickerView7.addBitImage(ImageUtils.getBitmap(listPath.get(6)));
                break;
            case 8:
                mDataBinding.ivStickerView1.addBitImage(ImageUtils.getBitmap(listPath.get(0)));
                mDataBinding.ivStickerView2.addBitImage(ImageUtils.getBitmap(listPath.get(1)));
                mDataBinding.ivStickerView3.addBitImage(ImageUtils.getBitmap(listPath.get(2)));
                mDataBinding.ivStickerView4.addBitImage(ImageUtils.getBitmap(listPath.get(3)));
                mDataBinding.ivStickerView5.addBitImage(ImageUtils.getBitmap(listPath.get(4)));
                mDataBinding.ivStickerView6.addBitImage(ImageUtils.getBitmap(listPath.get(5)));
                mDataBinding.ivStickerView7.addBitImage(ImageUtils.getBitmap(listPath.get(6)));
                mDataBinding.ivStickerView8.addBitImage(ImageUtils.getBitmap(listPath.get(7)));
                break;
            case 9:
                mDataBinding.ivStickerView1.addBitImage(ImageUtils.getBitmap(listPath.get(0)));
                mDataBinding.ivStickerView2.addBitImage(ImageUtils.getBitmap(listPath.get(1)));
                mDataBinding.ivStickerView3.addBitImage(ImageUtils.getBitmap(listPath.get(2)));
                mDataBinding.ivStickerView4.addBitImage(ImageUtils.getBitmap(listPath.get(3)));
                mDataBinding.ivStickerView5.addBitImage(ImageUtils.getBitmap(listPath.get(4)));
                mDataBinding.ivStickerView6.addBitImage(ImageUtils.getBitmap(listPath.get(5)));
                mDataBinding.ivStickerView7.addBitImage(ImageUtils.getBitmap(listPath.get(6)));
                mDataBinding.ivStickerView8.addBitImage(ImageUtils.getBitmap(listPath.get(7)));
                mDataBinding.ivStickerView9.addBitImage(ImageUtils.getBitmap(listPath.get(8)));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            String path = data.getStringExtra(Content.imagePath);
            if (requestCode == Content.SPLICE_ADD) {
                listPath.add(0, path);
                setSticker();
            }
        }
    }

    private void clearSelection() {
        mDataBinding.ivFreeSpliceKind1Sel.setVisibility(View.INVISIBLE);
        mDataBinding.ivFreeSpliceKind2Sel.setVisibility(View.INVISIBLE);
        mDataBinding.ivFreeSpliceKind3Sel.setVisibility(View.INVISIBLE);
    }

}